package com.crimerank;

import com.crimerank.Stations;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Home extends Activity implements LocationListener {

	LocationManager locationManager;
	TextView textLocation;
	Location lastLocation;

	private static final String PROVIDER_NAME = LocationManager.GPS_PROVIDER;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		textLocation = (TextView) findViewById(R.id.textLocation);

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		lastLocation = locationManager.getLastKnownLocation(PROVIDER_NAME);
		if (lastLocation != null) {
			onLocationChanged(lastLocation);
		}

	}

	public void onClick(View v) {
		Intent myIntent;

		// Give a default value in case GPS is unavailable.
		double lat = 38.894005612581736;
		double lon = -77.03660488128662;

		if (lastLocation != null) {
			// If GPS has a last known location, provide that instead.
			lat = lastLocation.getLatitude();
			lon = lastLocation.getLongitude();
		}

		// Store lat/lon pair in a Bundle to be passed with the Intent.
		Bundle loc = new Bundle();
		loc.putDouble("lat", lat);
		loc.putDouble("lon", lon);

		switch (v.getId()) {
		case (R.id.buttonNearestStation):
			myIntent = new Intent(v.getContext(), Stations.class);
			myIntent.putExtra("loc", loc);
			startActivityForResult(myIntent, 0);
			break;
		case (R.id.buttonNearestStop):
			myIntent = new Intent(v.getContext(), Stops.class);
			myIntent.putExtra("loc", loc);
			startActivityForResult(myIntent, 0);
			break;
		case (R.id.buttonRailIncidents):
			myIntent = new Intent(v.getContext(), Incidents.class);
			myIntent.putExtra("loc", loc);
			startActivityForResult(myIntent, 0);
			break;
		case (R.id.buttonMap):
			myIntent = new Intent(v.getContext(), Map.class);
			myIntent.putExtra("loc", loc);
			myIntent.putExtra("mapType", "single");
			startActivityForResult(myIntent, 0);
			break;

		}

	}

	@Override
	protected void onResume() {
		super.onRestart();
		locationManager.requestLocationUpdates(PROVIDER_NAME, 1000, 10, this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		String text = String.format(
				"Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f",
				location.getLatitude(), location.getLongitude(),
				location.getAltitude(), location.getBearing());
		textLocation.setText(text);
		lastLocation = location;
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}